﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NB_Controller_Test
{
    /// <summary>
    /// DevItemView.xaml 的交互逻辑
    /// </summary>
    public partial class DevItemView : UserControl
    {
        public const Byte byNotOtaing = 0;
        public const Byte byIsOtaing = 1;
        public const Byte byOTASuccess = 2;
        public const Byte byOTAFail = 3;
        public const Byte byOTATimeout = 4;

        public DevItemView()
        {
            InitializeComponent();

            //this.onOffBtn.Checked += new RoutedEventHandler(Off_CmdClick);
            //this.onOffBtn.Unchecked += new RoutedEventHandler(On_CmdClick);
        }

        //public void SetOTAState(byte byState)
        //{
        //    switch(byState)
        //    {
        //        case byNotOtaing:
        //            {
        //                //pbOTAing.Visibility = System.Windows.Visibility.Collapsed;
        //                //imgOTAState.Visibility = System.Windows.Visibility.Collapsed;
        //            }
        //            break;
        //        case byIsOtaing:
        //            {
        //                //pbOTAing.Visibility = System.Windows.Visibility.Visible;
        //                //imgOTAState.Visibility = System.Windows.Visibility.Collapsed;
        //            }
        //            break;
        //        case byOTASuccess:
        //            {
        //                //pbOTAing.Visibility = System.Windows.Visibility.Collapsed;
        //                //imgOTAState.Visibility = System.Windows.Visibility.Visible;
        //                //imgOTAState.Source = new BitmapImage(new Uri(@"Resource\Right.ico", UriKind.Relative));//strImagePath 就绝对路径 
        //            }
        //            break;
        //        case byOTAFail:
        //            {
        //                //pbOTAing.Visibility = System.Windows.Visibility.Collapsed;
        //                //imgOTAState.Visibility = System.Windows.Visibility.Visible;
        //                //imgOTAState.Source = new BitmapImage(new Uri(@"Resource\Error.ico", UriKind.Relative));//strImagePath 就绝对路径 
        //            }
        //            break;
        //        case byOTATimeout:
        //            {
        //                //pbOTAing.Visibility = System.Windows.Visibility.Collapsed;
        //                //imgOTAState.Visibility = System.Windows.Visibility.Visible;
        //                //imgOTAState.Source = new BitmapImage(new Uri(@"Resource\Error.ico", UriKind.Relative));//strImagePath 就绝对路径 
        //            }
        //            break;
        //        default:
        //            break;
        //    }
        //}

        public void SetGprsState()
        {
            double dSetJingDu = GlobalFun.StringToDouble(tb_Set_JingDu.Text);
            double dSetWeiDu = GlobalFun.StringToDouble(tb_Set_WeiDu.Text);

            double dReadJingDu = GlobalFun.StringToDouble(tb_Read_JingDu.Text);
            double dReadWeiDu = GlobalFun.StringToDouble(tb_Read_WeiDu.Text);
            //if (Math.Abs(dSetJingDu - dReadJingDu) <= 0.000001f && Math.Abs(dSetWeiDu - dReadWeiDu) <= 0.000001f)
            //{
            //
            //}

            //if(tb_Set_JingDu.Text == tb_Read_JingDu.Text && tb_Set_WeiDu.Text == tb_Read_WeiDu.Text)
            if (Math.Abs(dSetJingDu - dReadJingDu) <= 0.000001f && Math.Abs(dSetWeiDu - dReadWeiDu) <= 0.000001f)
            {
                sp_Set_JingWeiDu.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xD8, 0xEC, 0xD1));//FFD8ECD1

                tbHeader.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xD8, 0xEC, 0xD1));//FFD8ECD1
            }
            else
            {
                sp_Set_JingWeiDu.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xF5, 0xD3, 0xC9));//FFF5D3C9

                tbHeader.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xF5, 0xD3, 0xC9));//FFF5D3C9
            }
        }

        private void SendRequreDevVersionCmd()
        {
            //SendRequreVerCmd
            MainWindow mainWnd = App.Current.MainWindow as MainWindow;
            if (mainWnd != null)
            {
                IntPtr intPtr = (IntPtr)Tag;
                mainWnd.mainUIEx.SendRequreVerCmd(intPtr);
            }
        }

        private void On_CmdClick(object sender, RoutedEventArgs e)
        {
            //SendOnOffCmd
            MainWindow mainWnd = App.Current.MainWindow as MainWindow;
            if (mainWnd != null)
            {
                IntPtr intPtr = (IntPtr)Tag;
                mainWnd.mainUIEx.SendOnOffCmd(true, intPtr);
            }
        }

        private void Off_CmdClick(object sender, RoutedEventArgs e)
        {
            MainWindow mainWnd = App.Current.MainWindow as MainWindow;
            if (mainWnd != null)
            {
                IntPtr intPtr = (IntPtr)Tag;
                mainWnd.mainUIEx.SendOnOffCmd(false, intPtr);
            }
        }

        private void BtnReset(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定要重启吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                MainWindow mainWnd = App.Current.MainWindow as MainWindow;
                if (mainWnd != null)
                {
                    IntPtr intPtr = (IntPtr)Tag;
                    mainWnd.mainUIEx.SendResetCmd(intPtr);
                }
            }            
        }

        private void cbClick(object sender, RoutedEventArgs e)
        {
            MainWindow mainWnd = App.Current.MainWindow as MainWindow;
            if(mainWnd != null)
            {                
                mainWnd.mainUIEx.ShowLineGraphByIp(this.ip.Text.ToString(), check.IsChecked == true);
                mainWnd.mainUIEx.bAutoFitToView = true;
            }
        }

        private void aliasName_KeyUp(object sender, KeyEventArgs e)
        {
            //捕获回车按键，回车时写入配置文件
            if(e.Key == Key.Enter)
            {
                string strName = aliasName.Text;

                MainWindow mainWnd = App.Current.MainWindow as MainWindow;
                if (mainWnd != null)
                {
                    IntPtr intPtr = (IntPtr)Tag;
                    mainWnd.mainUIEx.UpdateAliasName(mac.Text, strName);
                }
            }
        }
    }
}
